<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class sermon_model extends CI_Model {
	//모델 생성자 호출
	function __construct(){
		parent::__construct();
		$this->load->helper('file');
	}
	
	/**
	 * 설교방송 내용 조회
	 *
	 * @access	public
	 * @return	array
	 */
	function select($id){
		$this->db->select("*");
	  	$this->db->where('sermon_id', $id);
	  	$query = $this->db->get('ch_sermon');
	  	//echo $this->db->last_query();
	  	$row = $query->row_array();
	  	
	  	if($row){
	  		$data['dbjob'] = 'u';
	  		$data['sermon_id'] = $id; 
	  		$data['title'] = $row['title'];
	  		$data['series'] = $row['series'];
	  		$data['airdate'] = substr($row['airdate'],0,10);
	  		$data['worship'] = $row['worship'];
	  		$data['runtime'] = $row['runtime'];
	  		$data['bible'] = $row['bible'];
	  		$data['preacher'] = $row['preacher'];
	  		$data['list_image'] = $row['list_image'];
	  		$data['vod_url'] = $row['vod_url'];
	  		$data['open_yn'] = $row['open_yn'];
	  		$data['title'] = $row['title'];
	  		
	  		//$content = html_content($html);
	  		$data['content'] = $row['content'];
	  	}else{
	  		$data['dbjob'] = 'i';
	  		$data['sermon_id'] = '';
	  		$data['series'] = '';
	  		$data['airdate'] = '';
	  		$data['worship'] = '';
	  		$data['runtime'] = '';
	  		$data['bible'] = '';
	  		$data['preacher'] = '';
	  		$data['list_image'] = '';
	  		$data['vod_url'] = '';
	  		$data['open_yn'] = 'Y';
	  		$data['title'] = '';
	  		$data['content'] = '';
	  	}
	  	return $data;
	}
	
	/**
	 * 설교방송 검색조건
	 *
	 * @access	public
	 * @return	query where절 구문
	 */
	function list_where($param){
		$keyname = $param['keyname'];
		$keyword = $param['keyword'];
	
		if($keyname && $keyword){
			$array_keyname = explode(',',$keyname);
			foreach($array_keyname as $key){
				$_where[] = "{$key} LIKE '%{$keyword}%'";
			}
			$where = implode(' OR ', $_where);
			return $this->db->where($where);
		}else{
			return null;
		}
	}
	
	/**
	 * 설교방송 db 처리
	 *
	 * @access	public
	 * @return	void
	 */
	function dbjob($data){
		$dbjob = $this->input->post('dbjob');
		$id = $this->input->post('id');
		switch($dbjob){
			case('i'):
				$this->db->insert('ch_sermon',$data);
			break;
			case('u'):
				$this->db->where('sermon_id',$id);
				$this->db->update('ch_sermon',$data);
			break;
			case('d'):
				$this->db->where('sermon_id',$id);
				$this->db->delete('ch_sermon');
			break;
		}
	}
	
	/**
	 * 설교방송 총갯수
	 *
	 * @access	public
	 * @return	integer
	 */
	function list_total($param){
		$this->list_where($param);
		$count = $this->db->count_all_results('ch_sermon');
		return $count;
	}
	
	/**
	 * 설교방송 리스트
	 *
	 * @access	public
	 * @return	result
	 */
	function sermon_list($param){
		$page = $param['page'];
		$listnum = $this->config->item('ch_listnum');
		$offset = $listnum*($page-1);
		$this->list_where($param);
		$this->db->order_by('sermon_id DESC');
		$query = $this->db->get('ch_sermon',$listnum,$offset);
		//echo $this->db->last_query();
		return $query->result();
	}
}
?>